const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },


  checkLogin() {
    console.log('APP-checkLogin');
    var isLogin = wx.getStorageSync("isAiMinLogin");
    if (!isLogin) {
      console.log("登录");
      // 登录
      wx.login({
        success: res => {
          console.log(res.code);
          // 发送 res.code 到后台换取 openId, sessionKey, unionId
          var url = app.globalData.url + "user/login";
          app.wxRequest(url, {
            code: res.code,
            doctorId: app.globalData.myDoctorId,
          }, res => {
            console.log(res);
            if (res.flag) {
              if (res.data.user.birth != null) {
                res.data.user.age = this.jsGetAge(res.data.user.birth)
              }
              app.globalData.user = res.data.user;
              app.globalData.userId = res.data.user.userId;
              app.globalData.userType = res.data.user.userType;
              app.globalData.openId = res.data.user.openId;
              wx.setStorageSync('user', JSON.stringify(res.data.user))
              var doctor = res.data.doctor || ''
              wx.setStorageSync('doctor', JSON.stringify(doctor))
              if (res.data.doctor != null) {
                app.globalData.doctorId = res.data.doctor.doctorId
                app.globalData.doctor = res.data.doctor
              }
              app.globalData.openId = res.data.user.openId;
              wx.setStorageSync('isAiMinLogin', true)
              this.checkUser(res.data.user)
            }
          }, err => {})
        }
      })
    } else {
      console.log("有信息");
      var user = JSON.parse(wx.getStorageSync('user'))
      var doctor = JSON.parse(wx.getStorageSync('doctor'))
      if (doctor != '') {
        app.globalData.doctor = doctor
        app.globalData.doctorId = doctor.doctorId
      }
      console.log(user);
      if (user.birth != null) {
        user.age = this.jsGetAge(user.birth)
      }
      app.globalData.user = user;
      app.globalData.userId = user.userId;
      app.globalData.userType = user.userType;
      app.globalData.openId = user.openId;

      console.log(app.globalData);

      console.log(isLogin);

      this.checkUser(user)
    }
  },

  checkUser(user) {
    if (user.userName == null) {
      if (app.globalData.myDoctorId != 0) {
        // 扫码进入
        wx.redirectTo({
          url: '/user/pages/userInfo/userInfo',
        })
        return
      }
      wx.showModal({
        title: '提示',
        content: '请选择您的身份',
        cancelText: '医生',
        confirmText: '用户',
        complete: (res) => {
          if (res.confirm) {
            wx.redirectTo({
              url: '/user/pages/userInfo/userInfo',
            })
          }
          if (res.cancel) {
            app.globalData.isDoctorFirst = true
            wx.redirectTo({
              url: '/doctor/pages/updateDoctorInfo/updateDoctorInfo',
            })
          }
        }
      })
    } else {
      wx.switchTab({
        url: '/pages/index/index',
      })
    }
  },
  jsGetAge(strBirthday) {
    var returnAge;
    var strBirthdayArr = strBirthday.split("-");
    var birthYear = strBirthdayArr[0];
    var birthMonth = strBirthdayArr[1];
    var birthDay = strBirthdayArr[2];

    var d = new Date();
    var nowYear = d.getFullYear();
    var nowMonth = d.getMonth() + 1;
    var nowDay = d.getDate();

    if (nowYear == birthYear) {
      returnAge = 0; //同年 则为0岁
    } else {
      var ageDiff = nowYear - birthYear; //年之差
      if (ageDiff > 0) {
        if (nowMonth == birthMonth) {
          var dayDiff = nowDay - birthDay; //日之差
          if (dayDiff < 0) {
            returnAge = ageDiff - 1;
          } else {
            returnAge = ageDiff;
          }
        } else {
          var monthDiff = nowMonth - birthMonth; //月之差
          if (monthDiff < 0) {
            returnAge = ageDiff - 1;
          } else {
            returnAge = ageDiff;
          }
        }
      } else {
        returnAge = -1;
      }
    }

    return returnAge;

  },







  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.checkLogin()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})